using System;
using System.Collections.Generic;
using System.Text;
using WCSFContrib.PageFlow;
using System.Collections;

namespace PageFlowQuickstart.Infrastructure.PageFlow
{
    public class SimplePageFlowInstanceStoreProvider : IPageFlowInstanceStore
    {
        IPageFlow _pageFlow;

        #region IPageFlowInstanceStoreProvider Members

        public SimplePageFlowInstanceStoreProvider(string connectionString, IPageFlowCorrelationTokenProvider tokenProvider)
        {
        }

        public void Add(IPageFlow pageFlowInstance)
        {
            if (_pageFlow != null)
                throw new InvalidOperationException("No more than one page flow instance allowed");

            _pageFlow = pageFlowInstance;
        }

        public void Remove(IPageFlow pageFlowInstance)
        {
            if (_pageFlow != null && _pageFlow.Id.Equals(pageFlowInstance.Id))
            {
                _pageFlow = null;
            }
        }

        public void Remove(Guid id)
        {
            if (_pageFlow != null && _pageFlow.Id.Equals(id))
            {
                _pageFlow = null;
            }
        }

        public Guid GetLastRunningInstance()
        {
            if (_pageFlow == null)
            {
                return Guid.Empty;
            }
            else
            {
                return _pageFlow.Id;
            }
        }

        public Guid GetByType(Type type)
        {
            if (_pageFlow == null)
            {
                return Guid.Empty;
            }
            else
            {
                return _pageFlow.Id;
            }
        }

        public void SetPageFlowRunning(IPageFlow pageFlow)
        {

        }

        public void SetPageFlowNotRunning(IPageFlow pageFlow)
        {
            
        }

        public string GetInstanceType(Guid id)
        {
            if (_pageFlow != null && _pageFlow.Id.Equals(id))
            {
                return _pageFlow.Definition.PageFlowType.AssemblyQualifiedName;
            }
            return null;
        }

        #endregion        
    }
}
